<?php

namespace App\Http\Controllers\Admin;

use App\Model\Secify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class SceifyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = Secify::orderBy('id','desc')->paginate(15);
        return view('admin.place.place-classify-list',compact('item'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.place.place-classify-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required'
        ],[
            'name.required'=>'请填写基地名称'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        $data = Secify::create(['classify' => $request->input('name')]);
        if ($data){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $single = Secify::where('id',$id)->first();
        return view('admin.place.place-classify-edit',compact('single'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required'
        ],[
            'name.required'=>'请填写基地名称'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        $data = Secify::find($id);
        $data->classify = $request->input('name');
        if ($data->save()){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Secify::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
